local zhini = fk.CreateSkill{
  name = "xiaobai__zhini",
}

Fk:loadTranslationTable{
  ["xiaobai__zhini"] = "指逆",
  [":xiaobai__zhini"] = "准备阶段，你可以选择一名其他角色并依次执行下列操作："..
                        "展示其三张牌；重铸其两张牌；弃置其一张牌，（不足则改为全部手牌）"..
                        "然后若此过程中：出现过三张伤害牌，你对其造成一点伤害；三次均出现伤害牌，其翻面。",
  ["#xiaobai__zhini_discard"] = "指逆：弃置 %dest 一张牌",
  ["#xiaobai__zhini_recast"] = "指逆：重铸 %dest 两张牌",
  ["#xiaobai__zhini_show"] = "指逆：展示 %dest 三张牌",
  ["#xiaobai__zhini_prompt"] = "指逆：选择一名角色",

  ["$xiaobai__zhini1"] = "公欲宽其缚而用其能，岂忘丁原董卓之事乎？",
  ["$xiaobai__zhini2"] = "布虽败，其众仍伏于野，不可恕之。",
}

zhini:addEffect(fk.EventPhaseStart, {
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target == player and player.phase == Player.Start
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local t = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player), 
      min_num = 1, 
      max_num = 1,
      prompt = "#xiaobai__zhini_prompt",
      skill_name = self.name,
      no_indicate = false,
    })
    if t and #t>0 then
      event:setCostData(self, t[1])
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local to = event:getCostData(self)  ---@type ServerPlayer
    local cards
    local room = player.room
    local cardsNum = to:getHandcardNum()
    local hurt_num = 0
    local hurt_times = 0
    local has_hurt = false
    cards = room:askToChooseCards(player,{
      target = to,
      min = math.min(cardsNum, 3),
      max = math.min(cardsNum, 3),
      flag = "he",
      skill_name = self.name,
      prompt = "#xiaobai__zhini_show::"..to.id})
    to:showCards(cards)
    if player.dead or to.dead then return end
    for _, cid in ipairs(cards) do
      if Fk:getCardById(cid).is_damage_card then
        if hurt_num ~= -1 then
          hurt_num = hurt_num + 1
        end
        if not has_hurt then
          has_hurt = true
          hurt_times = hurt_times + 1
        end
      end
    end
    has_hurt = false
    if hurt_num >= 3 then
      room:damage{
        from = player,
        to = to,
        damage = 1,
        skillName = self.name,
      }
      hurt_num = -1
    end
    local cardsNum = to:getHandcardNum()
    room:delay(500)
    cards = room:askToChooseCards(player, {
      target = to,
      min = math.min(cardsNum, 2),
      max = math.min(cardsNum, 2), 
      flag = "he",
      skill_name = self.name,
      prompt = "#xiaobai__zhini_recast::"..to.id
    })
    for _, cid in ipairs(cards) do
      if Fk:getCardById(cid).is_damage_card then
        if hurt_num ~= -1 then
          hurt_num = hurt_num + 1
        end
        if not has_hurt then
          has_hurt = true
          hurt_times = hurt_times + 1
        end
      end
    end
    
    has_hurt = false
    room:recastCard(cards, to, self.name) 
    if hurt_num >= 3 then
      room:damage{
        from = player,
        to = to,
        damage = 1,
        skillName = self.name,
      }
      hurt_num = -1
    end
    if player.dead or to.dead then return end
    local cardsNum = to:getHandcardNum()
    room:delay(500)
    cards = room:askToChooseCards(player,{
      target = to,
      flag = "he",
      min = math.min(cardsNum, 1),
      max = math.min(cardsNum, 1),
      skill_name = self.name,
      prompt = "#xiaobai__zhini_discard::"..to.id,
    })
    for _, cid in ipairs(cards) do
      if Fk:getCardById(cid).is_damage_card then
        if hurt_num ~= -1 then
          hurt_num = hurt_num + 1
        end
        if not has_hurt then
          has_hurt = true
          hurt_times = hurt_times + 1
        end
      end
    end
    has_hurt = false
    room:throwCard(cards, self.name, to, player)
    if hurt_num >= 3 then
      room:damage{
        from = player,
        to = to,
        damage = 1,
        skillName = self.name,
      }
      hurt_num = -1
    end
    if hurt_times == 3 then
      to:turnOver()
    end
  end
})

return zhini